# -*- coding: utf-8 -*-

import os

from langchain.schema import (HumanMessage, SystemMessage)
from langchain_openai import ChatOpenAI

# 设置环境变量
os.environ["OPENAI_API_KEY"] = os.getenv("DASHSCOPE_API_KEY")
os.environ["OPENAI_API_BASE"] = "https://dashscope.aliyuncs.com/compatible-mode/v1"

try:
    # 创建ChatOpenAI实例   # 使用Qwen-Plus模型
    chat = ChatOpenAI(model_name="qwen-plus",
                      openai_api_key=os.getenv("OPENAI_API_KEY"),
                      openai_api_base=os.getenv("OPENAI_API_BASE"),
                      temperature=0.7,  # 可选参数，设置生成文本的随机性
                      )

    # 定义消息
    messages = [SystemMessage(content="You are a helpful assistant."), HumanMessage(content="你是谁？")]

    # 发送请求
    response = chat.invoke(messages)

    # 打印回复
    print(response.content)
except Exception as e:
    print(f"错误信息：{e}")
    print("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code")
